package com.qingke.easyjava.jdbc.command.impl;

import java.util.List;

import com.qingke.easyjava.jdbc.app.BeSuperDao;
import com.qingke.easyjava.jdbc.command.PlayerCommand;
import com.qingke.easyjava.jdbc.pojo.Answer;
import com.qingke.easyjava.jdbc.pojo.Player;
import com.qingke.easyjava.jdbc.pojo.Question;
import com.qingke.easyjava.jdbc.util.QingkeConsole;

public class AnswerCommand extends PlayerCommand{

	public AnswerCommand(Player playerProfile) {
		super(playerProfile);
	}

	@Override
	public void execute() {
		
		int qid = QingkeConsole.askUserInputInt("Please enter question ID");
		
		BeSuperDao dao = new BeSuperDao();
		Question question = dao.getQuestion(qid);;
		
		if(question == null) {
			QingkeConsole.println("Invalid question ID!");
			return;
		}
		
		
		if(question.getFrom() != null ) {
			if(question.getFrom().getId() == playerProfile.getId()) {
				QingkeConsole.println("You are not allowed to answer your question");
				return;
			}
		}
		QingkeConsole.println(question);
		if(question.isOpen()) {
			QingkeConsole.println("The question has been solved!");
		}
		
		String content = QingkeConsole.askUserInput("Please enter your answer:");
		Answer answer = new Answer(-1,content,playerProfile);
		answer.setQuestion(question);
		
		dao.upsertAnswer(answer);
		
		List<Answer> dList = dao.getAnswersFrom(playerProfile);
		playerProfile.setAnswers(dList);
	}

}
